﻿using System;
using System.Data;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Collections.Generic;

namespace MagicCard
{
    public class AccessDao
    {
        public OleDbConnection conn;

        public AccessDao(OleDbConnection srcConn)
        {
            conn = srcConn;
        }

        public bool IsDoubleName(string name)
        {
            string commStr = "select ID from MC_Card where [CardName] = '" + name + "'";
            conn.Open();
            OleDbCommand comm = new OleDbCommand(commStr, conn);
            try
            {
                object obj = comm.ExecuteScalar();
                if (obj == null)
                {
                    return false;
                }
                int count;
                if (int.TryParse(obj.ToString(), out count))
                {
                    if (count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }


        public DataTable GetStarsFromCategory()
        {
            string commStr = "SELECT DISTINCT Stars FROM MC_Category";
            DataTable value = new DataTable();
            try
            {
                conn.Open();
                OleDbDataAdapter oda = new OleDbDataAdapter(commStr, conn);
                oda.Fill(value);
            }
            catch (Exception ex)
            {

            }
            finally
            {
                conn.Close();
            }
            return value;
        }

        public DataTable GetCategoryByStars(string stars)
        {
            string commStr = "SELECT ID,CategoryName FROM MC_Category WHERE  (Stars = @Stars)";
            OleDbParameter parameter = new OleDbParameter("@Stars", stars);
            OleDbCommand comm = new OleDbCommand(commStr, conn);
            comm.Parameters.Add(parameter);
            DataTable value = new DataTable();
            try
            {
                conn.Open();
                OleDbDataAdapter oda = new OleDbDataAdapter(comm);
                oda.Fill(value);
            }
            catch (Exception ex)
            {

            }
            finally
            {
                conn.Close();
            }
            return value;
        }

        public DataTable GetBaseCardByCategory(string categoryName)
        {
            string commStr = @"SELECT MC_Card.ID, MC_Card.CardName, MC_Card.CardValue 
                                 FROM (MC_Card INNER JOIN MC_Category ON MC_Card.CardCategory = MC_Category.ID) 
                                WHERE (MC_Card.CardLv = 0) 
                                  AND (MC_Category.CategoryName = @CategoryName)";
            OleDbParameter parameter = new OleDbParameter("@CategoryName", categoryName);
            OleDbCommand comm = new OleDbCommand(commStr, conn);
            comm.Parameters.Add(parameter);
            DataTable value = new DataTable();
            try
            {
                conn.Open();
                OleDbDataAdapter oda = new OleDbDataAdapter(comm);
                oda.Fill(value);
            }
            catch (Exception ex)
            {

            }
            finally
            {
                conn.Close();
            }
            return value;
        }

        public DataTable GetBaseCardsForComposite(string categoryName, int cardLv)
        {
            string commStr = @"SELECT MC_Card.ID, MC_Card.CardName 
                                 FROM (MC_Card INNER JOIN MC_Category ON MC_Card.CardCategory = MC_Category.ID) 
                                WHERE (MC_Card.CardLv < "
                                      + cardLv + ")" +
                                  "AND (MC_Category.CategoryName = @CategoryName)";
            OleDbParameter parameter = new OleDbParameter("@CategoryName", categoryName);
            OleDbCommand comm = new OleDbCommand(commStr, conn);
            comm.Parameters.Add(parameter);
            DataTable value = new DataTable();
            try
            {
                conn.Open();
                OleDbDataAdapter oda = new OleDbDataAdapter(comm);
                oda.Fill(value);
            }
            catch (Exception ex)
            {

            }
            finally
            {
                conn.Close();
            }
            return value;
        }

        public DataTable GetCardInfoByCategory(int categoryID)
        {
            string commStr = @"SELECT T.ID, T.CardName,T.CardLv, T.CardValue, T.CardTime, T.Requirements,T1.CardName as Base1 ,T2.CardName as Base2 ,T3.CardName as Base3
                                                   FROM  ((MC_Card T left join MC_Card T1 on T1.ID=T.Base1) left join MC_Card T2 on T2.ID=T.Base2) left join MC_Card T3 on T3.ID=T.Base3 
                                                WHERE T.CardCategory=" + categoryID + " order by T.CardLv desc ";
            OleDbCommand comm = new OleDbCommand(commStr, conn);
            DataTable value = new DataTable();
            try
            {
                conn.Open();
                OleDbDataAdapter oda = new OleDbDataAdapter(comm);
                oda.Fill(value);
            }
            catch (Exception ex)
            {

            }
            finally
            {
                conn.Close();
            }
            return value;
        }

        public void InitCardInfo(int categoryID)
        {
            string commStr = "UPDATE MC_Card SET Requirements = 1 where CardCategory = " + categoryID;
            OleDbCommand comm = new OleDbCommand(commStr, conn);
            try
            {
                conn.Open();
                int val = comm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }

        public bool Check(int categoryID, out string message)
        {
            int maxLv;
            string commStr = "select max(CardLv) from MC_Card where CardCategory = " + categoryID;
            OleDbCommand comm = new OleDbCommand(commStr, conn);
            try
            {
                conn.Open();
                object obj = comm.ExecuteScalar();
                if (obj != null)
                {
                    if (int.TryParse(obj.ToString(), out maxLv))
                    {
                        if (maxLv == 1)
                        {
                            message = "无合成卡片信息！";
                            return false;
                        }
                        if (maxLv == 0)
                        {
                            message = "无卡片信息！";
                            return false;
                        }
                    }
                    else
                    {
                        message = "无卡片信息！";
                        return false;
                    }
                }
                else
                {
                    message = "无卡片信息！";
                    return false;
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
            message = "";
            return true;
        }

        public bool Calculate(int categoryID)
        {
            int maxLv = 0;
            string commStr = "select max(CardLv) from MC_Card where CardCategory = " + categoryID;
            OleDbCommand comm = new OleDbCommand(commStr, conn);
            try
            {
                conn.Open();
                object obj = comm.ExecuteScalar();
                maxLv = int.Parse(obj.ToString());
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
            for (int i = maxLv; i > 0; i--)
            {
                this.CalculateOneLv(categoryID, i);
            }

            return true;
        }

        private void CalculateOneLv(int categoryID, int lv)
        {
            string commStr = @"SELECT T.ID, T.Requirements,T.Base1 ,T.Base2 ,T.Base3 
                                                    FROM MC_Card T
                                                  WHERE T.CardCategory = " + categoryID + " AND T.CardLv = " + lv;
            OleDbCommand comm = new OleDbCommand(commStr, conn);
            DataTable srcData = new DataTable();
            try
            {
                conn.Open();
                OleDbDataAdapter oda = new OleDbDataAdapter(comm);
                oda.Fill(srcData);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
            for (int i = 0; i < srcData.Rows.Count; i++)
            {
                int base1 = int.Parse(srcData.Rows[i]["Base1"].ToString());
                int base2 = int.Parse(srcData.Rows[i]["Base2"].ToString());
                int base3 = int.Parse(srcData.Rows[i]["Base3"].ToString());
                int req = int.Parse(srcData.Rows[i]["Requirements"].ToString());
                this.UpdateOneCard(base1, base2, base3, req);

            }
        }

        private void UpdateOneCard(int cardID1, int cardID2, int cardID3, int req)
        {
            string commStr = @"UPDATE MC_CARD 
                                                    SET Requirements = Requirements + " + req +
                                          " WHERE ID=" + cardID1 + " or ID=" + cardID2 + " or ID=" + cardID3;
            OleDbCommand comm = new OleDbCommand(commStr, conn);
            try
            {
                conn.Open();
                int val = comm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }
    }
}
